const utils = require('../../utils.js')
const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')

Component({
  properties: {
    currentItem: {
      type: Object,
      value:{},
      observer(newValue, oldValue) {
        if (!!this.data.currentItem && typeof this.data.currentItem == 'object') {
          this.currentItemFn()
        }  
      }
    },
    notBj:Boolean,
    editState:{
      type:Boolean,
      value:false
    },
    linkState:{
      type:Boolean,
      value:true
    },
    likeList: {
      type: Object,
      value: '',
      observer(newValue, oldValue) {

      }
    },
    notLike:{
      type:Boolean,
      value:false
    },
    isUser:{
      type: Boolean,
      value: false
    }
  },
  data: {
    currentJson:{},
    isLiked:'',
    allLikeState:false,
    curLikeCount:0,
    allCLassState:false
  },
  methods: {
    // 点击点赞判断调用
    switchPraise(e){
      if (this.data.notLike ){
        this.setData({
          allLikeState: !this.data.allLikeState
        })
        return false;
      }
      this.data.isLiked ? this.CancelPraise(e.currentTarget.dataset.cpcid) : this.like(e.currentTarget.dataset.cpcid)
    },

    // 我的班级打卡跳转
    myClocnFn(e) {
      wx.navigateTo({
        url: `/pages/myClockList/myClockList?uId=${e.currentTarget.dataset.uid}`
      })
    },
    // 全局捕获
    bindViewTap(event) {
      this.setData({
        allLikeState: false
      })

      this.setData({
        allCLassState: false
      })
    },

    // 点赞
    like(id) {
      utils.ajaxFn({
        url: config.service.likeCard,
        body:{ 
          cpcId: id
        },
        callback: (data) => {
          this.switchIsLiked()
        }
      })

      
    },

    // 取消点赞
    CancelPraise(id) {
      utils.ajaxFn({
        url: config.service.unlikeCard,
        body: {
          cpcId: id
        },
        callback: (data) => {
          this.switchIsLiked()
        }
      })
    },

    // 切换点赞操作
    switchIsLiked() {
      this.setData({
        isLiked: !this.data.isLiked
      })
      if (!!this.data.currentItem.isLiked && !!this.data.isLiked) {
        this.setData({
          curLikeCount: this.data.curLikeCount + 1
        })
      } else if (!!this.data.currentItem.isLiked && !this.data.isLiked ){
        this.setData({
          curLikeCount: this.data.curLikeCount - 1
        })
      }

      if (!this.data.currentItem.isLiked && !!this.data.isLiked) {
        this.setData({
          curLikeCount: this.data.curLikeCount + 1
        })
      } else if (!this.data.currentItem.isLiked && !this.data.isLiked) {
        this.setData({
          curLikeCount: this.data.curLikeCount - 1
        })
      }

      
    },

    // 切换图标
    classSwitch() {
      this.setData({
        allCLassState: !this.data.allCLassState
      })
    },

    // 打卡详情
    ClockFn(e) {
      if (e.target.id != '' || !this.data.linkState) return ;

      wx.navigateTo({
        url: `/pages/particulars/particulars?cpcId=${e.currentTarget.dataset.cpcid}&type=${e.currentTarget.dataset.type}`
      })
    },

    // 初始化参数
    currentItemFn() {
      this.data.currentItem['timeString'] = utils.GetDateStr(this.data.currentItem['createTime'])

      this.setData({
        currentJson: this.data.currentItem
      })

      this.setData({
        isLiked: this.data.currentItem.isLiked
      })


      this.setData({
        curLikeCount: Number(this.data.currentItem.likeCount)
      })
    },

    //打卡编辑
    editFn(e) {
      wx.navigateTo({
        url: `/pages/editClickClock/editClickClock?cpcId=${e.currentTarget.dataset.cpcid}&ciid=${e.currentTarget.dataset.ciid}`
      })
    },

    // 删除打卡
    deleteFn(e) {
      utils.ajaxFn({
        url: config.service.unlikeCard,
        body: {
          cpcId: e.currentTarget.dataset.cpcid
        },
        callback: (data) => {
          wx.showToast({
            title: '删除成功！',
            duration: 2000
          })

          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }
      })
    }
  }
})